//-----------------------------------------------------------------------
// <copyright file="ProjectPresenter.cs" company="Simnova LLC">
//     Copyright (c) Simnova LLC. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Simnova.Collaborate.Presenter
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class ProjectPresenter
    {
        /*
         * To ReGenerate the ProjectService.cs file, use the following command from 
         * the VisualStudio 2005 Command Prompt while the Windows Service is runnning.
         * svcutil /language:C# /config:app.config http://localhost:8080/ProjectService/mex
        */

        private readonly View.IProject projectView;
        private int companyId;

        public ProjectPresenter(View.IProject projectView, int companyId)
        {
            this.companyId = companyId;
            this.projectView = projectView;
            this.projectView.CompanyId = this.companyId;
            this.projectView.ProjectName = String.Empty;
            this.projectView.ProjectDescription = String.Empty;
            this.LoadProjects();
        }

        public void LoadProjects()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.projectView.ProjectList = collaborateClient.GetProjectsByCompany(this.companyId);
            collaborateClient.Close();
        }

        public void LoadProject(int projectId)
        {
            // Load the project from the application server using WCF
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            AppServer.Project project = collaborateClient.GetProject(projectId);
            collaborateClient.Close();

            if (project == null)
            {
                project = new AppServer.Project();
            }

            if (project.ProjectId != 0)
            {
                this.projectView.ProjectName = project.ProjectName;
                this.projectView.ProjectDescription = project.ProjectDescription;
            }
            else
            {
                this.projectView.ProjectName = "not found";
                this.projectView.ProjectDescription = "nothing";
            }
        }

        public void Save()
        {
            // Ensure our project object is in sync with the view.
            AppServer.Project project = new AppServer.Project();
            project.CompanyId = this.companyId;
            project.ProjectName = this.projectView.ProjectName;
            project.ProjectDescription = this.projectView.ProjectDescription;

            // Let the application server take care of saving the object. 
            // (this will happen over the network)
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            collaborateClient.SaveProject(project);
            collaborateClient.Close();
        }
    }
}
